﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using WFNetLib.ADO;
using System.Data;

namespace WebApplication1.Page
{
    public partial class FrameworkManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string zx = "", fj = "", yh = "", id = "";
            if (!IsPostBack)
            {
                btNew.Enabled = false;
                if (Roles.IsUserInRole("系统管理员"))
                {
                    string url;
                    url = "~/Page/FrameworkEdit.aspx?id=&zx=" + zx + "&fj=" + fj + "&yh=" + yh;
                    TreeNode xt_tn = new TreeNode("系统平台");
                    TreeNode new_zx_tn=new TreeNode("$新建中心$","","",url,"_parent");
                    xt_tn.ChildNodes.Add(new_zx_tn);
                    string strSql = "SELECT   ID, 名称, IsAvailable"
                                + " FROM      结构分级"
                                + " WHERE   (中心ID IS NULL)";
                    DataTable xt_dt = SQLServerOP.DataTableSQL(strSql);                   
                    foreach (DataRow zx_dr in xt_dt.Rows)
                    {
                        结构分级Data zx_data = 结构分级DataFactory.Construct(zx_dr);
                        id = zx_data.ID.ToString();
                        zx = "";
                        fj = "";
                        url = "~/Page/FrameworkEdit.aspx?id="+id+"&zx=" + zx + "&fj=" + fj + "&yh=" + yh;
                        TreeNode zx_tn = new TreeNode(zx_data.名称, zx_data.ID.ToString(), "", url, "_parent");
                        zx = zx_data.ID.ToString();
                        fj = "";
                        url = "~/Page/FrameworkEdit.aspx?id=&zx=" + zx + "&fj=" + fj + "&yh=" + yh;
                        TreeNode new_fj_tn = new TreeNode("$新建分局$", "", "", url, "_parent");
                        zx_tn.ChildNodes.Add(new_fj_tn);
                        strSql = "SELECT   ID, 名称, IsAvailable"
                                + " FROM      结构分级"
                                + " WHERE   (分局ID IS NULL) AND (中心ID='"+zx_data.ID.ToString()+"')";
                        DataTable zx_dt = SQLServerOP.DataTableSQL(strSql);
                        foreach (DataRow fj_dr in zx_dt.Rows)
                        {
                            结构分级Data fj_data = 结构分级DataFactory.Construct(fj_dr);
                            id = fj_data.ID.ToString();
                            zx = zx_data.ID.ToString();
                            fj = "";
                            url = "~/Page/FrameworkEdit.aspx?id=" + id + "&zx=" + zx + "&fj=" + fj + "&yh=" + yh;
                            TreeNode fj_tn = new TreeNode(fj_data.名称, fj_data.ID.ToString(), "", url, "_parent");
                            fj = fj_data.ID.ToString();
                            url = "~/Page/FrameworkEdit.aspx?id=&zx=" + zx + "&fj=" + fj + "&yh=" + yh;
                            TreeNode new_ptyh_tn = new TreeNode("$新建普通用户$", "", "", url, "_parent");
                            fj_tn.ChildNodes.Add(new_ptyh_tn);
                            strSql = "SELECT   ID, 名称, IsAvailable"
                                    + " FROM      结构分级"
                                    + " WHERE  (分局ID='"+fj_data.ID.ToString()+"') AND (中心ID='" + zx_data.ID.ToString() + "')";
                            DataTable fj_dt = SQLServerOP.DataTableSQL(strSql);
                            foreach (DataRow ptyh_dr in fj_dt.Rows)
                            {
                                结构分级Data ptyh_data = 结构分级DataFactory.Construct(ptyh_dr);
                                id = ptyh_data.ID.ToString();
                                zx = zx_data.ID.ToString();
                                fj = fj_data.ID.ToString();
                                url = "~/Page/FrameworkEdit.aspx?id=" + id + "&zx=" + zx + "&fj=" + fj + "&yh=" + yh;
                                TreeNode ptyh_tn = new TreeNode(ptyh_data.名称, ptyh_data.ID.ToString(), "", url, "_parent");
                                fj_tn.ChildNodes.Add(ptyh_tn);
                            }
                            zx_tn.ChildNodes.Add(fj_tn);
                        }
                        xt_tn.ChildNodes.Add(zx_tn);
                    }             
                    TreeView1.Nodes.Add(xt_tn);
                }
                if (Roles.IsUserInRole("普通用户"))
                {
                    DropDownList1.Items[1].Enabled = false;
                    DropDownList1.Items[2].Enabled = false;
                    DropDownList1.Items[3].Enabled = false;                  
                }
                else if(Roles.IsUserInRole("分局管理员"))
                {
                    DropDownList1.Items[1].Enabled=false;
                    DropDownList1.Items[2].Enabled=false;
                }
                else if(Roles.IsUserInRole("中心管理员"))
                {
                    DropDownList1.Items[1].Enabled = false;
                }
            }
        }
        static Guid[] items;
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string zx = "", fj = "", yh = "", id = "";
            if (DropDownList1.Text == "中心信息")
            {
                if(Roles.IsUserInRole("中心管理员_新增"))
                    btNew.Enabled = true;
                string strSql = "SELECT   ID, 名称, 地址, 备注, Creator AS 创建人, AddTime AS 添加时间, UpdateTime AS 最后更新数据, IsAvailable"
                                + " FROM      结构分级"
                                + " WHERE   (中心ID IS NULL)";
                DataTable dt = SQLServerOP.DataTableSQL(strSql);
                items = new Guid[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    items[i] = Guid.Parse(dt.Rows[i]["ID"].ToString());
                }
                dt.Columns.Remove("ID");
                GridView1.DataSource = dt;
                GridView1.DataBind();
                zx = "";
                fj = "";
                yh = "";
            }
            else if (DropDownList1.Text == "分局信息")
            {
                if (Roles.IsUserInRole("分局管理员_新增"))
                    btNew.Enabled = true;
                string strSql = "SELECT   结构分级.ID, 结构分级.名称, 结构分级.地址, 结构分级.备注, 结构分级.IsAvailable, 结构分级.Creator, 结构分级.AddTime, "
                                   + " 结构分级.UpdateTime, 结构分级_1.名称 AS 中心名称"
                    + " FROM      结构分级 AS 结构分级_1 INNER JOIN"
                      + " 结构分级 ON 结构分级_1.ID = 结构分级.中心ID"
                    + " WHERE   (结构分级_1.分局ID IS NULL)";
                DataTable dt = SQLServerOP.DataTableSQL(strSql);
                items = new Guid[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    items[i] = Guid.Parse(dt.Rows[i]["ID"].ToString());
                }
                dt.Columns.Remove("ID");
                GridView1.DataSource = dt;
                GridView1.DataBind();
                zx = "";
                fj = "";
                yh = "";
            }
        }

        protected void btNew_Click(object sender, EventArgs e)
        {
            string zx = "", fj = "", yh = "", id = "";
            Response.Redirect("~/Page/FrameworkEdit.aspx?id=&zx=" + zx + "&fj=" + fj + "&yh=" + yh);
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string zx = "", fj = "", yh = "";
            if (DropDownList1.Text == "中心信息")
            {
                zx = "";
                fj = "";
                yh = "";
            }
            Response.Redirect("~/Page/FrameworkEdit.aspx?id=" + items[e.NewEditIndex].ToString() + "&zx=" + zx + "&fj=" + fj + "&yh=" + yh);
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            结构分级Data d = new 结构分级Data();
            d.ID = items[e.RowIndex];
            结构分级DataDBOption.Delete(d);
            Response.Redirect("~/Page/FrameworkManage.aspx");
        }

    }
}